package phoenix.web.test_objects;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseWebExt;
import phoenix.core.data_models.UserProfileDataModel.AboutMe;
import phoenix.core.data_models.UserProfileDataModel.LookingFor;
import phoenix.core.data_models.UserProfileDataModel.ProfileDetails;
import phoenix.web.test_objects.data_providers.MyProfileDataProviders;

public class MyProfileTests extends TestBaseWebExt
{

	public MyProfileTests(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	public MyProfileDataProviders dataProviders = new MyProfileDataProviders(manager);

	/**
	 * Save data after funnel test
	 * @param p_looking
	 * @param p_about
	 */
	public void saveDataAfterFunnelTest(LookingFor p_looking, AboutMe p_about)
	{
		try
		{
			wd().openAutologin(testUser());
			wd().openPage("/profile");
			page().myProfilePage().waitForPageLoad();

			test().assertEqualsWithScreen(page().myProfilePage().getScreenname(), testUser().getScreenname(), "Save screnname after funnel");
			test().assertEqualsWithScreen(page().myProfilePage().getDescription(), p_looking.description, "Save description after funnel");

			if (manager.isBN() || manager.isCNF() || manager.isFlirt() || manager.isLGW() || manager.isNF() || manager.isWB() || manager.isMD())
			{
				if (manager.isMD())
				{
					page().myProfilePage().clickEditLookingFor();
					wd().wait(1);
				}
				test().assertEqualsWithScreen(page().myProfilePage().getLookingGender(), p_looking.gender, "Save gender after funnel");
				test().assertEqualsWithScreen(page().myProfilePage().getLookingAgeFrom(), p_looking.ageFrom, "Save age from after funnel");
				test().assertEqualsWithScreen(page().myProfilePage().getLookingAgeTo(), p_looking.ageTo, "Save age to after funnel");
				test().assertEqualsWithScreen(page().myProfilePage().getLookingLocation(), p_looking.location, "Save location after funnel");
			}

			if (manager.isCNF() || manager.isLGW() || manager.isNF() || manager.isMD())
			{
				test().assertEqualsWithScreen(page().myProfilePage().getLookingDistance(), p_looking.distance, "Save distance after funnel");
			}

			if (manager.isFGF())
			{
				test().assertEqualsWithScreen(page().myProfilePage().getHeight(), p_about.height, "Save height after funnel");
				test().assertEqualsWithScreen(page().myProfilePage().getWeight(), p_about.weight, "Save weight after funnel");
				test().assertEqualsWithScreen(page().myProfilePage().getMaritalStatus(), p_about.maritalStatus, "Save marital status after funnel");
				test().assertEqualsWithScreen(page().myProfilePage().getNativeLanguage(), p_about.nativeLanguage, "Save native language after funnel");
				test().assertEqualsWithScreen(page().myProfilePage().getSecondLanguage(), p_about.secondLanguage, "Save second language after funnel");
				test().assertEqualsWithScreen(page().myProfilePage().getHobbies(), p_about.hobbiesAndInterests, "Save hobbies and interests after funnel");
			}
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("saveDataAfterFunnelTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Update main info test
	 * @param p_details
	 */
	public void updateMainInfoTest(ProfileDetails p_details)
	{
		try
		{
			wd().openAutologin(testUser());

			if (!wd().getCurrentUrl().contains("profile"))
			{
				wd().openPage("/profile");
			}

			page().myProfilePage().waitForPageLoad();

			if (manager.isBN() || manager.isWB())
			{
				page().myProfilePage().clickEditMainInfo();
			}

			if (manager.isCNF() || manager.isLGW() || manager.isNF())
			{
				page().myProfilePage().clickEditAge();
				page().myProfilePage().clickEditStatus();
				page().myProfilePage().clickEditAbout();
				page().myProfilePage().clickEditLocation();
			}

			page().myProfilePage().setBirthDay(p_details);
			page().myProfilePage().setBirthMonth(p_details);
			page().myProfilePage().setBirthYear(p_details);
			page().myProfilePage().setLocation(p_details);
			page().myProfilePage().setDescription(p_details);
			page().myProfilePage().setAboutMe(p_details);

			if (manager.isBN() || manager.isWB() || manager.isFlirt() || manager.isMD())
			{
				page().myProfilePage().clickSaveMainInfo();
			}

			if (manager.isCNF() || manager.isLGW() || manager.isNF())
			{
				page().myProfilePage().clickSaveAge();
				page().myProfilePage().clickSaveLocation();
				page().myProfilePage().clickSaveStatus();
				page().myProfilePage().clickSaveAbout();
			}

			wd().wait(30);
			wd().refreshPage();
			page().myProfilePage().waitForPageLoad();

			test().assertEqualsWithScreen(page().myProfilePage().getAge(), p_details.age, "Age display on myProfilePage");
			test().assertEqualsWithScreen(page().myProfilePage().getLocation(), p_details.location, "Location display on myProfilePage");
			test().assertEqualsWithScreen(page().myProfilePage().getDescription(), p_details.description, "Description display on myProfilePage");
			test().assertEqualsWithScreen(page().myProfilePage().getAboutMe(), p_details.about, "About me display on myProfilePage");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("updateMainInfoTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Update personal info test
	 * @param p_details
	 */
	public void updatePersonalInfoTest(ProfileDetails p_details)
	{
		try
		{
			wd().openAutologin(testUser());

			if (!wd().getCurrentUrl().contains("profile"))
			{
				wd().openPage("/profile");
			}

			page().myProfilePage().waitForPageLoad();

			if (manager.isBN() || manager.isWB() || manager.isMD())
			{
				page().myProfilePage().clickEditPersonalInfo();
			}

			if (manager.isLGW())
			{
				page().myProfilePage().clickEditAppearance();
			}

			if (manager.isCNF() || manager.isNF())
			{
				page().myProfilePage().clickEditAppearance();
				page().myProfilePage().clickEditLifestyle();
				page().myProfilePage().clickEditBackground();
			}

			page().myProfilePage().setPersonalInfo(p_details);

			if (manager.isBN() || manager.isWB() || manager.isFlirt() || manager.isMD())
			{
				page().myProfilePage().clickSavePersonalInfo();
			}

			if (manager.isLGW())
			{
				page().myProfilePage().clickSaveAppearance();
			}

			if (manager.isCNF() || manager.isNF())
			{
				page().myProfilePage().clickSaveAppearance();
				page().myProfilePage().clickSaveLifestyle();
				page().myProfilePage().clickSaveBackground();
			}

			wd().wait(30);
			wd().refreshPage();
			page().myProfilePage().waitForPageLoad();

			if (manager.isMD())
			{
				page().myProfilePage().clickEditPersonalInfo();
			}

			test().assertEqualsWithScreenPersonalInfo(page().myProfilePage().getPersonalInfo(), p_details, "myProfilePage");
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("updatePersonalInfoTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Update looking for info test
	 */
	public void updateLookingForInfoTest(LookingFor p_looking)
	{
		try
		{
			wd().openAutologin(testUser());

			if (!wd().getCurrentUrl().contains("profile"))
			{
				wd().openPage("/profile");
			}

			page().myProfilePage().waitForPageLoad();

			if (manager.isBN() || manager.isWB() || manager.isMD())
			{
				page().myProfilePage().clickEditLookingForInfo();
			}

			if (manager.isCNF() || manager.isLGW() || manager.isNF())
			{
				page().myProfilePage().clickEditLookingFor();
			}

			page().myProfilePage().setLookingGender(p_looking);
			page().myProfilePage().setLookingAgeFrom(p_looking);
			page().myProfilePage().setLookingAgeTo(p_looking);
			page().myProfilePage().setLookingLocation(p_looking);

			if (manager.isCNF() || manager.isLGW() || manager.isNF() || manager.isMD())
			{
				page().myProfilePage().setLookingDistance(p_looking);
			}

			if (manager.isBN() || manager.isWB() || manager.isFlirt() || manager.isMD())
			{
				page().myProfilePage().clickSaveLookingForInfo();
			}

			if (manager.isCNF() || manager.isLGW() || manager.isNF())
			{
				page().myProfilePage().clickSaveLookingFor();
			}

			wd().wait(30);
			wd().refreshPage();
			page().myProfilePage().waitForPageLoad();

			if (manager.isMD())
			{
				page().myProfilePage().clickEditLookingFor();
			}

			test().assertEqualsWithScreen(page().myProfilePage().getLookingGender(), p_looking.gender.toLowerCase(),
				"Looking for gender display on myProfilePage");
			test().assertEqualsWithScreen(page().myProfilePage().getLookingAgeFrom(), p_looking.ageFrom, "Looking for age from display on myProfilePage");
			test().assertEqualsWithScreen(page().myProfilePage().getLookingAgeTo(), p_looking.ageTo, "Looking for age to display on myProfilePage");
			test().assertEqualsWithScreen(page().myProfilePage().getLookingLocation(), p_looking.location, "Looking for location display on myProfilePage");

			if (manager.isCNF() || manager.isLGW() || manager.isNF() || manager.isMD())
			{
				test().assertEqualsWithScreen(page().myProfilePage().getLookingDistance(), p_looking.distance, "Looking for distance display on myProfilePage");
			}
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("updateLookingForInfoTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Upload photo test
	 */
	public void uploadPhotoTest()
	{
		if (manager.testModel().getBrowserName().equals(manager.config().BROWSER_IEXPLORER))
		{
			log().warn("uploadPhotoTest temporarily disabled for BROWSER_IEXPLORER");
			return;
		}

		try
		{
			wd().openAutologin(testUser());

			if (!wd().getCurrentUrl().contains("profile"))
			{
				wd().openPage("/profile");
			}

			page().myProfilePage().waitForPageLoad();

			if (manager.isBN() || manager.isWB())
			{
				page().myProfilePage().clickUploadPhotoMenu();
			}

			page().myProfilePage().uploadPhoto();

			page().myProfilePage().waitForPhotoElement();
			if (!page().myProfilePage().isPhotoPresent())
			{
				log().errorAndScreen("Photo image isn't uploaded");
			}
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("uploadPhotoTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * Remove photo test
	 */
	public void removePhotoTest()
	{
		if (manager.testModel().getBrowserName().equals(manager.config().BROWSER_IEXPLORER))
		{
			log().warn("removePhotoTest temporarily disabled for BROWSER_IEXPLORER");
			return;
		}

		try
		{
			wd().openAutologin(testUser());
			wd().wait(2);
			if (!wd().getCurrentUrl().contains("profile"))
			{
				wd().openPage("/profile");
			}

			page().myProfilePage().waitForPageLoad();

			if (manager.isBN() || manager.isWB())
			{
				wd().wait(2);
				page().myProfilePage().clickUploadPhotoMenu();
			}

			page().myProfilePage().removePhoto();
			wd().wait(2);
			if (page().myProfilePage().isPhotoPresent())
			{
				log().errorAndScreen("Photo wasn't removed from pop-up?");
			}
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("removePhotoTest: " + p_ex.toString());
		}

		test().forErrors();
	}

	/**
	 * View profile details other user test
	 * @param p_details
	 * @param p_looking
	 * @param p_about
	 */
	public void viewDetailsOtherUserTest(ProfileDetails p_details, LookingFor p_looking, AboutMe p_about)
	{
		try
		{
			wd().openAutologin(testUser());
			wd().openPage("/user/view/id/" + testUser().getId());
			page().userPage().waitForPageLoad();

			// Main Info test
			test().assertEqualsWithScreen(page().userPage().getScreenname(), testUser().getScreenname(), "Screenname display on userPage");
			test().assertEqualsWithScreen(page().userPage().getAge(), p_details.age, "Age display on userPage");
			test().assertEqualsWithScreen(page().userPage().getLocation(), p_details.location.substring(0, 7), "Location display on userPage");

			// Personal Info test
			if (manager.isMD())
			{
				page().userPage().clickPersonalInfoBlock();
			}

			test().assertEqualsWithScreenPersonalInfo(page().userPage().getPersonalInfo(), p_details, "userPage");

			// Looking for Info test
			if (manager.isMD())
			{
				page().userPage().clickLookingForBlock();
			}

			test().assertEqualsWithScreen(page().userPage().getLookingGender(), p_looking.gender, "Looking for gender display on userPage");
			test().assertEqualsWithScreen(page().userPage().getLookingAgeFrom(), p_looking.ageFrom, "Looking for age from display on userPage");
			test().assertEqualsWithScreen(page().userPage().getLookingAgeTo(), p_looking.ageTo, "Looking for age to display on userPage");

			// Bug #143758: User Page: looking for: incorrect displayed location
			// test().assertEqualsWithScreen(page().userPage().getLookingLocation(), p_looking.location,
			// "Looking for location display on userPage");

			if (manager.isNF())
			{
				test().assertEqualsWithScreen(page().userPage().getLookingDistance(), p_looking.distance, "Looking for distance display on userPage");
			}
		}

		catch (Exception p_ex)
		{
			log().errorAndScreen("viewDetailsOtherUserTest: " + p_ex.toString());
		}

		test().forErrors();
	}
}